<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Company\Archive;
use App\Models\Company\Furniture;
use App\Models\Company\Material;
use App\Models\Encyclopedia\Article;
use App\Models\Label;
use App\Models\System\Region as RegionModel;
use Zhuzhichao\IpLocationZh\Ip;

class LabelController extends Controller
{
    public function index()
    {
        #UPDATE label SET `is_distinct`=1 where name='娄底建材公司';
        $label = Label::where('is_state', true)->where('is_distinct', 0)->paginate(600);
        return view('label', compact('label'));
    }

    public function find($id)
    {
        $label = Label::find($id);
        if (!$label) {
            abort(404);
        }
        $article = Article::inRandomOrder()->take(7)->get();

        $article = $article->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });

        $strategy = Article::inRandomOrder()->take(6)->get();

        $strategy = $strategy->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });

        $zhishi = Article::inRandomOrder()->take(6)->get();

        $zhishi = $zhishi->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        $region = config('region.region.model', '');
        $materials = Material::with(['region', 'category'])
            ->when($region != '', function ($query) use ($region) {
                $query->where('region_id', $region->id);
            })
            ->where('is_state', true)->take(7)->get();
        $furniture = Furniture::with(['region', 'category'])
            ->when($region != '', function ($query) use ($region) {
                $query->where('region_id', $region->id);
            })
            ->where('is_state', true)->take(7)->get();
        $archive = Archive::with(['region'])
            ->when($region != '', function ($query) use ($region) {
                $query->where('region_id', $region->id);
            })
            ->where('is_state', true)->take(7)->get();

        $archive = Archive::with('region')->where('is_state', true)->take(6)->get();
        return view('biaoqian', compact('label', 'article', 'strategy', 'zhishi', 'archive', 'data', 'materials', 'furniture', 'archive', 'region'));
    }
}
